<?php 

/**
* 
*/
class ku1
{
	public $db;
	public $table;
	public $error;
	public $errno;
	function __construct($table=null)
	{
		$this->table=$table;
		try{
		$this->db = new PDO("mysql:host=127.0.0.1;dbname=school1;charset=utf8","root","");
		}catch(PDOException  $e ){
			echo "Error: ".$e;
		}
	}
	//查询表数据
	public function query($sql)
	{
		$res = $this->db->query($sql);
		if(!$res){
					echo "执行失败：错误代码：".$this->db->errorCode()."错误信息：".$this->db->errorInfo()[2];
				}
				return $res->fetchAll(PDO::FETCH_ASSOC);
	}
	public function add($data)
	{
		// var_dump($data);
		$values= implode('","',$data);
		$keys = implode(',', array_keys($data));
		// var_dump($keys);
		// var_dump($values);
		$sql = 'insert into '.$this->table.' ('.$keys.') values ("'.$values.'")';
		// var_dump($sql);
		return $this->exec($sql);
	}
	public function exec($sql)
	{
		$res = $this->db->exec($sql);
		var_dump($res);
		if (!$res) {
			$this->error=$this->db->errorInfo()[2];
			$this->errno=$this->db->errorCode();
		}
		return $res;
	}
	public function error()
	{
		return "错误信息：".$this->error."错误代码：".$this->errno;
	}
}








 ?>